package handler

import (
	"github.com/gin-gonic/gin"
	"net/http"
	"time"
	"weekly-exam/api/request"
	"weekly-exam/consts"
	"weekly-exam/intermal/dao"
	"weekly-exam/intermal/model"
	"weekly-exam/pkg"
)

func Login(c *gin.Context) {
	var req request.UserReq
	var err error
	if err = c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code":    http.StatusBadRequest,
			"message": "验证失败",
			"data":    false,
		})
		return
	}
	var u model.User
	dao.GetOneFile(&model.User{Mobile: req.Mobile}, &u)
	if u.Id == 0 {
		c.JSON(http.StatusOK, gin.H{
			"code":    http.StatusBadRequest,
			"message": "没找到此用户",
			"data":    false,
		})
		return
	}
	token := pkg.CreateToken(uint(u.Id), 24*time.Hour, consts.JWT_KEY)
	c.JSON(http.StatusOK, gin.H{
		"code":    http.StatusOK,
		"message": "成功",
		"data":    token,
	})
	return
}
